import { Injectable } from '@angular/core';
import { URLSearchParams, RequestOptions } from '@angular/http';
import { BASEURL } from "shared";
import { CmHttp } from "./../../http-module/http.service";

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/toPromise';

// interface
import { CompanyInfo } from '../comp-profile/comp-profile.interface';

@Injectable()
export class F10Service {
  constructor ( private http: CmHttp ) {}

  private getWithCache(url: string, options: RequestOptions) {
    return this.http.get(
      url, options, {cacheAge: this.http.CACHE_AGE.Hour}
    )
  }

  private handleError (error: Response | any) {
    let errMsg: string;
    if (error instanceof Response) {
      //const body = error.json() || '';
      const err = error.errCode || JSON.stringify(error);
      errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
    } else {
      errMsg = error.message ? error.message : error.toString();
    }
    // return Observable.throw(errMsg);
    return Observable.empty();
  }

  /**
   * 0 => 操盘必读
   * @param symbol
   */
  // 操盘必读 => 主要指标
  getMainindexes (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/mainindexes`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }

  /**
   * 1 => comp-profile板块接口
   * @param symbol
   */
  // 公司概况
  getCompanyinfo (type, symbol): Observable<CompanyInfo> {
    let url = `${BASEURL}/stock/newinfo/${type}`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  
  /**
   * 2 => manage版块接口
   * @param symbol
   */
  // 主营介绍 todo ===>>> 产品经理要求暂取公司概况中的数据
  getCompIntro (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/companyinfo`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  // 主营构成
  getMainoperincome (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/mainoperincome`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  // 经营评述
  getOperstatus (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/operstatus`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }

  /**
   * 3 => news板块接口
   * @param symbol
   * @param category
   */
  // 公司公告列表
  getAnnouncements ({symbol, category='all', page, perPage}): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/announcements`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    if (category !== 'all') {
      params.set('category', category)
    }
    if (page) {
      params.set('page', page)
    }
    if (perPage) {
      params.set('perPage', perPage)
    }
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  // 公告原文件
  getAnnouncementsPdf (id): string {
    let url = `${BASEURL}/stock/newinfo/announcements/${id}.pdf`
    return url
  }
  // 新闻列表
  getNews ({symbol, page, perPage}): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/news`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    if (page) {
      params.set('page', page)
    }
    if (perPage) {
      params.set('perPage', perPage)
    }
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  // 新闻详情
  getNewinfo (newId): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/news/${newId}`
    return this.getWithCache(url)
      .catch(this.handleError)
  }

  /**
   * 5 => managers板块接口
   * @param symbol
   */
  // 公司高管
  getLeadersin (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/leaders`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  // 高管持股变动
  getLeaderstockalters (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/leaderstockalters`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }

  /**
   * 6 => capital 板块接口
   */
  // 募集资金
  getPlacements (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/placements`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  // 项目进度
  getInvests (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/invests`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  // 收购兼并
  getMnas (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/mnas`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }

  /**
   * 7 => shareholder 板块接口
   * @param symbol
   */
  // 股东人数
  getShareholdernum (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/shareholdernum`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  // 十大股东
  getTop10shareholders (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/top10shareholders`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }

  /**
   * 8 => share 板块接口
   * @param symbol
   */
  // 股本结构
  getSharestruct (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/sharestruct`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  // 限售解禁
  getUnfreezeStock ({symbol, page, perPage}): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/floatingschedules`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    if (page) {
      params.set('page', page)
    }
    if (perPage) {
      params.set('perPage', perPage)
    }
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }

  /**
   * 10 => finance板块接口
   * @param symbol
   */
  // 财务分析
  getFinacialAnalysis (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/financialanalysis`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  // // 资产负债表
  // getAssets (symbol): Observable<any> {
  //   let url = `${BASEURL}/stock/newinfo/balancesheetall`
  //   let params = new URLSearchParams()
  //   params.set('symbol', symbol)
  //   return this.getWithCache(url, {search: params})
  //     .map(this.extractData)
  //     .catch(this.handleError)
  // }
  // // 利润表
  // getIncome (symbol): Observable<any> {
  //   let url = `${BASEURL}/stock/newinfo/incomeall`
  //   let params = new URLSearchParams()
  //   params.set('symbol', symbol)
  //   return this.getWithCache(url, {search: params})
  //     .map(this.extractData)
  //     .catch(this.handleError)
  // }
  // // 现金流量表
  // getCashflow (symbol): Observable<any> {
  //   let url = `${BASEURL}/stock/newinfo/cashflowall`
  //   let params = new URLSearchParams()
  //   params.set('symbol', symbol)
  //   return this.getWithCache(url, {search: params})
  //     .map(this.extractData)
  //     .catch(this.handleError)
  // }
  // 业绩预告
  getPerfforecast (symbol): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/perfforecast`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  /**
   * 11 => event 板块接口
   */
  // 大事提醒
  getEventtips ({symbol, page, perPage}): Observable<any> {
    let url = `${BASEURL}/stock/newinfo/eventtips`
    let params = new URLSearchParams()
    params.set('symbol', symbol)
    if (page) {
      params.set('page', page)
    }
    if (perPage) {
      params.set('perPage', perPage)
    }
    return this.getWithCache(url, {search: params})
      .catch(this.handleError)
  }
  // 重大事项
  // 大宗交易
  // 融资融卷
}

